﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.XtraCharts;

namespace Lamex.mMachine.UI
{
    public partial class frmFAMA_WarPow_Data_Analysis : frmBaseChild
    {
        string strD1, strD2;
        string strSql;

        public frmFAMA_WarPow_Data_Analysis()
        {
            InitializeComponent();
        }

        private void frmFAMA_WarPow_Data_Analysis_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            strSql = @"SELECT COST_CENTER,READING_DATE,a.METER_NO,METER_READING,DIFF
                        FROM View_FAMA_WarPow_Cal_Diff AS a
                        LEFT JOIN
                        FAMA_WatPow_Meter_Distribute AS b
                        ON a.METER_NO=b.METER_NO";
            DataTable dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            gridControl1.DataSource = dt;
        }

        private void rbtnCurMonth_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCurMonth.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                dteStart.DateTime = now.AddDays(-now.Day + 1);
                dteEnd.DateTime = now.AddMonths(1).AddDays(-now.AddMonths(1).Day + 1).AddDays(-1);
            }
        }

        private void rbtnCurSeason_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnCurSeason.Checked == true)
            {
                doClearViewAndChartData();

                DateTime now = DateTime.Now;
                var time = now.AddMonths(0 - ((now.Month - 1) % 3));
                dteStart.DateTime = time.AddDays(-time.Day + 1);
                var time1 = now.AddMonths((3 - ((now.Month - 1) % 3) - 1));
                dteEnd.DateTime = time1.AddMonths(1).AddDays(-time1.AddMonths(1).Day + 1).AddDays(-1);
            }
        }

        //重置图表
        private void doClearViewAndChartData()
        {
            gridView1.Columns.Clear();
            chartControl1.Series.Clear();
            this.Text = "用水用电分析";
        }

        private void dteStart_Enter(object sender, EventArgs e)
        {
            rbtnCurMonth.Checked = false;
            rbtnCurSeason.Checked = false;
        }

        private void dteEnd_Enter(object sender, EventArgs e)
        {
            rbtnCurMonth.Checked = false;
            rbtnCurSeason.Checked = false;
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            //将原始读表记录转换实际用量
            //View_FAMA_WarPow_Cal_Diff原始SQL语句
//            strSql = @"WITH 
//                        t AS ( 
//                        SELECT READING_DATE,METER_NO,METER_READING,UPDATE_TIME,UPDATE_USER 
//	                          ,ROW_NUMBER() over(PARTITION BY METER_NO ORDER BY METER_READING)-1 AS 'NUM' 
//                        FROM FAMA_WatPow_Meter_Reading), 
//                        d AS ( 
//                        SELECT READING_DATE,METER_NO,METER_READING,UPDATE_TIME,UPDATE_USER 
//	                          ,ROW_NUMBER() over(PARTITION BY METER_NO ORDER BY METER_READING) AS 'NUM' 
//                        FROM FAMA_WatPow_Meter_Reading 
//                        ) 
//                        SELECT t.READING_DATE,t.METER_NO,t.METER_READING,t.UPDATE_TIME,t.UPDATE_USER, 
//	                        ISNULL( t.METER_READING-d.METER_READING,0) DIFF 
//                        FROM t 
//                        LEFT JOIN d on d.NUM=t.NUM 
//                        AND d.METER_NO=t.METER_NO 
//                        ORDER BY t.METER_NO,t.READING_DATE ";

            strSql = @"SELECT COST_CENTER,READING_DATE,a.METER_NO,METER_READING,DIFF
                        FROM View_FAMA_WarPow_Cal_Diff AS a
                        LEFT JOIN
                        FAMA_WatPow_Meter_Distribute AS b
                        ON a.METER_NO=b.METER_NO";

            DataTable dtTemp = null;
            dtTemp = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);

            gridControl1.DataSource = dtTemp;

            //gridView1.Columns.Clear();
            //getFilterDate();//获取起止时间段

        }

        //获取起止时间段
        private void getFilterDate()
        {
            strD1 = dteStart.Text.ToString();

            if (dteEnd.Text == "")
            {
                strD2 = strD1;
            }
            else
            {
                strD2 = dteEnd.Text.ToString();
            }
        }

        #region 加载图表数据

        private void setData_GridView(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                gridControl1.DataSource = dt;
                gridView1.OptionsBehavior.Editable = false;
                gridView1.OptionsView.ColumnAutoWidth = true;

                //refreshPositionStaduss();
            }
        }

        private void setData_ChartControl(DataTable dt, ChartControl chart, string strFeild_Key, string strFeild_Value)
        {
            chart.Series.Clear();

            if (dt.Rows.Count != 0)
            {
                Series series1 = new Series("", ViewType.Line);

                foreach (DataRow dr in dt.Rows)
                {
                    SeriesPoint p = new SeriesPoint();
                    double[] d = new double[] { double.Parse(Math.Round((decimal.Parse(dr[strFeild_Value].ToString())), 2).ToString()) };
                    p.Values = d;
                    setPointArgument(dr, strFeild_Key, p);
                    series1.Points.Add(p);
                }

                chart.Series.Add(series1);
                chart.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
                Legend Legends = (Legend)chart.Legend;
                Legends.Visibility = DevExpress.Utils.DefaultBoolean.True;
                Legends.AlignmentVertical = LegendAlignmentVertical.Top;
                Legends.AlignmentHorizontal = LegendAlignmentHorizontal.Left;
                Legends.TextVisible = true;

                //强制显示所有x轴标签
                XYDiagram diagram = ((XYDiagram)chart.Diagram);
                diagram.AxisX.QualitativeScaleOptions.AutoGrid = false;
                diagram.AxisX.Label.ResolveOverlappingOptions.AllowHide = false;

                //设置Series的标签
                series1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;//每个点都显示数据
            }
        }

        //排除因IE_WorkCenter_Definition、OP_MGT_Factory_Workstation、OP_MGT_Workstation_Process三个表中
        //PROCESS_CODE、WORKSTATION未同步定义，导致x轴标签出现NULL值，Argument报错，
        public void setPointArgument(DataRow dr, string strFeild_Key, SeriesPoint p)
        {
            if (dr[strFeild_Key].ToString() == "")
            {
                p.Argument = "NULL";
            }
            else
            {
                p.Argument = dr[strFeild_Key].ToString();
            }
        }

        #endregion

        #region 选项区与表复制操作

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 3;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 84;
        }

        //gridView仅复制单元格内容，复制选中行焦点单元格的值
        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & e.KeyCode == Keys.C)
            {
                Clipboard.SetDataObject(gridView1.GetFocusedRowCellValue(gridView1.FocusedColumn));
                e.Handled = true;
            }

            //或设置属性
            //optionBehavior - CopyToClipboardWithColumnHeader = false;
            //optionSelection - MultiSelectMode = CellSelect;
        }

        #endregion
    }
}